package activity.servlet;

import activity.dao.Actdao;
import activity.entity.Activity;
import activity.utils.JBDCUlit2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

@WebServlet("/upact")
public class updataAct extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String aname=req.getParameter("aname");
        String atime=req.getParameter("atime");
        String address=req.getParameter("address");
        String contactman=req.getParameter("contactman");
        String callnum=req.getParameter("callnum");
        String actid=req.getParameter("actid");

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out =resp.getWriter();

        JBDCUlit2 db=new JBDCUlit2();
        Activity activity=new Activity();

        activity.setAname(aname);
        activity.setAtime(atime);
        activity.setAddress(address);
        activity.setContactman(contactman);
        activity.setCallnum(callnum);
        activity.setActid(actid);

        Actdao dao=new Actdao();

        Connection conn=db.getConn();
        try {
            if (dao.upAct(conn,activity)){
                out.println(dao.upAct(conn,activity));
                out.println("<h1>修改成功</h1>");
                resp.setHeader("Refresh", "1;url=Actlist.jsp");
            }else {
                out.println(dao.upAct(conn,activity));
                out.println("<h1>没有此活动, 修改失败</h1>");
                resp.setHeader("Refresh", "1;url=upact.jsp");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
